from csr_utils import log

from dao import runner_dao
from orm_domain.runner import Runner
from utils.bean_utils import BeanUtils


class RunnerService:

    def save(self,runner:Runner):
        """
        保存runner
        @param runner:名称、command不弄输入空
        @return: runnerid
        """
        if BeanUtils.str_is_blank(runner.name):
            log.info("runner名称不能输入空")
        if BeanUtils.str_is_blank(runner.command):
            log.info("runner运行命令不能输入空")
        runner.name = runner.name.strip()
        runner.command = runner.command.strip()
        return runner_dao.save(runner)


    def update(self,runner:Runner):
        """
        更新runner
        @param runner: 名称、command不弄输入空
        @return:
        """
        queryrunner = runner_dao.get_by_id(runner.id)
        if runner_dao.get_by_id(runner.id) == None:
            log.info("未查到对应runner")
            return None
        if BeanUtils.str_is_blank(runner.name):
            log.info("runner名称不能输入空")
        if BeanUtils.str_is_blank(runner.command):
            log.info("runner运行命令不能输入空")
        queryrunner.name = runner.name.strip()
        queryrunner.command = runner.command.strip()
        runner_dao.update(queryrunner)

    def search_by_name(self,name) -> list:
        return runner_dao.get_list_by_name(name=name)

    def get_by_id(self,id) -> Runner:
        return runner_dao.get_by_id(id)

    def get_list(self):
        return runner_dao.get_list()

    def delete(self,id):
        runner = runner_dao.get_by_id(id)
        if runner == None:
            log.info("未查到对应runner")
            return None
        return runner_dao.delete(runner)